﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.IO;

namespace DataProcess {
	public partial class frmLetterConversion : Form {
		public frmLetterConversion() {
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e) {
			OleDbConnection clientConn = new OleDbConnection();
			OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
			builder.Provider = "Microsoft.Jet.OLEDB.4.0";
			string cString = textBox1.Text;
			if (String.IsNullOrEmpty(cString)) {
				MessageBox.Show("请选择数据库文件！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (cString.IndexOfAny(new char[] { '/', '\\' }) == -1) {
				cString = Path.Combine(Application.StartupPath, cString);
			}
			if (!File.Exists(cString)) {
				MessageBox.Show("数据库文件不存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			builder.DataSource = cString;
			clientConn.Close();
			clientConn.ConnectionString = builder.ConnectionString;
			try {
				clientConn.Open();
			}
			catch (Exception) {
				MessageBox.Show("不能打开数据库！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (clientConn.State != ConnectionState.Open) {
				MessageBox.Show("不能打开数据库！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}



			FDADataSetTableAdapters.FDATableAdapter adp = new FDADataSetTableAdapters.FDATableAdapter();
			adp.Connection = clientConn;
			var data = adp.GetData();
			for (int i = 0; i < data.Count; i++) {
				data[i].newName = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(data[i].FDAname.ToLower());
			}
			adp.Update(data);
			clientConn.Close();
			MessageBox.Show("完成！");
		}

		private void button2_Click(object sender, EventArgs e) {
			if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
				textBox1.Text = openFileDialog1.FileName;
			}
		}
	}
}
